<template>
  <view class="number-box">
    <button class="number-box__minus" v-show="!disabled && modelValue > min" @click="onClick(-1)">
      <image class="img" :src="$oss('/common/minus_on.png')" />
    </button>
    <button class="number-box__minus disabled" v-show="disabled || modelValue <= min">
      <image class="img" :src="$oss('/common/minus_off.png')" />
    </button>

    <view class="number-box__value">{{ modelValue }}</view>

    <button class="number-box__plus" v-show="!disabled && modelValue < max" @click="onClick(1)">
      <image class="img" :src="$oss('/common/plus_on.png')" />
    </button>
    <button class="number-box__plus disabled" v-show="disabled || modelValue >= max">
      <image class="img" :src="$oss('/common/plus_off.png')" />
    </button>
  </view>
</template>

<script>
export default {
  name: 'NumberBox',
  props: {
    modelValue: {
      type: [String, Number]
    },
    min: {
      type: [String, Number],
      default: 0
    },
    max: {
      type: [String, Number],
      default: 99
    },
    disabled: Boolean
  },
  emits: ['update:model-value', 'input', 'change', 'plus', 'minus'],
  setup (props, { emit }) {
    const emitChange = (value) => {
      emit('update:model-value', value)
      emit('input', value)
      emit('change', value)
    }

    const onClick = (num) => {
      let value = props.modelValue + num

      if (value < props.min) value = props.min
      if (value > props.max) value = props.max

      emitChange(value)
      emit(num > 0 ? 'plus' : 'minus')
    }

    return {
      onClick
    }
  }
}
</script>

<style lang="scss">
.number-box {
  display: flex;
  align-items: center;
  justify-content: center;

  &__minus,
  &__plus {
    width: 56rpx;
    height: 56rpx;
    border-radius: 0;
    font-size: 0;

    .img {
      width: 100%;
      height: 100%;
    }
  }

  &__minus {
    // @include oss-bg("/common/minus_on.png");

    &.disabled {
      // @include oss-bg("/common/minus_off.png");
    }
  }

  &__plus {
    // @include oss-bg("/common/plus_on.png");

    &.disabled {
      // @include oss-bg("/common/plus_off.png");
    }
  }

  &__value {
    width: 72rpx;
    height: 56rpx;
    line-height: 56rpx;
    text-align: center;
    font-size: 28rpx;
    font-family: PingFangSC, PingFangSC-Regular;
    color: #2b2b2b;
    @include oss-bg('/common/number_box_value.png');
  }
}
</style>
